
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace ComBrowser
{
	public partial class MainViewController : UIViewController
	{
		#region Constructors

		// The IntPtr and initWithCoder constructors are required for controllers that need 
		// to be able to be created from a xib rather than from managed code

		public MainViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public MainViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public MainViewController () : base("MainViewController", null)
		{
			Initialize ();
		}

		void Initialize ()
		{
		}
		
		#endregion
		
	    FlipperViewController flip = null;
        ComDomain comDomain = null;
		WebMainView webView;
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			this.WebView.SetController (this);
			
			//Set the toolbars as hidden
			this.NavigationController.SetNavigationBarHidden (true, false);
			this.NavigationController.SetToolbarHidden (true, false);
			
			comDomain = ComDomain.Instance ();
			PositionDatabaseAtLastReadPosition ();
			CustomizeTitleBar ();
			WireUpCommands ();
		}

		#region WireUpCommands
		private void WireUpCommands ()
		{
			btnNextChapter.Clicked += delegate { MoveToNextChapter (); };
			btnPreviousChapter.Clicked += delegate { MoveToPreviousChapter (); };
		}
		#endregion
		
		#region Helper: PositionDatabaseAtLastReadPosition
		void PositionDatabaseAtLastReadPosition ()
		{
			int b = Settings.ZeroBasedSelectedBookID;
			int c = Settings.ZeroBasedSelectedChapterID;
			
			if (!comDomain.BookHasChapter (b, c))
			{
				comDomain.GetVeryFirstBookChapter (ref b, ref c);
				Settings.ZeroBasedSelectedBookID = b;
				Settings.ZeroBasedSelectedChapterID = c;
			}
			
		}
		#endregion
		
		#region Helper: ToggleMenu
		bool toolBarVisible = false;
		public void ToggleMenu ()
		{
			CreateFlipChapterPicker ();
			this.NavigationController.SetNavigationBarHidden (toolBarVisible, true);
			this.NavigationController.SetToolbarHidden (toolBarVisible, true);
			toolBarVisible = !toolBarVisible;
		}
        #endregion
		
		#region Helper: CreateFlipChapterPicker
		void CreateFlipChapterPicker ()
		{
			if (flip == null)
			{
				flip = new FlipperViewController (this);
				float left = (UIScreen.MainScreen.ApplicationFrame.Width - flip.View.Frame.Width) / 2;
				float top = (UIScreen.MainScreen.ApplicationFrame.Height - flip.View.Frame.Height) / 2;
				flip.View.Frame = new System.Drawing.RectangleF (left, top, flip.View.Frame.Width, flip.View.Frame.Height);
				flip.View.Hidden = true;
				this.View.AddSubview (flip.View);
			}
		}
		#endregion
		
		#region Helper: CustomizeTitleBar
		void CustomizeTitleBar ()
		{
			UIBarButtonItem btnVerse = new UIBarButtonItem ();
			btnVerse.Title = "Chapter";
			btnVerse.Clicked += delegate {
				this.ToggleMenu ();
				flip.View.Hidden = false;
			};
			this.NavigationItem.SetRightBarButtonItem (btnVerse, false);
		}
		#endregion

		#region Event: ViewWillAppear (bool animated)
		public override void ViewWillAppear (bool animated)
		{
			WebView.Delegate = new WebMainViewDelegate (this);
			ShowChapter (Settings.ZeroBasedSelectedBookID, Settings.ZeroBasedSelectedChapterID); 	 
		}
		#endregion
		
		#region Helper: ShowChapter
		void ShowChapter (int ZeroBasedBookID, int ZeroBasedChapterID)
		{
			
			NSUrl path = new NSUrl (NSBundle.MainBundle.BundlePath);
			
			string clearEffects = @"
				<script>
				document.documentElement.style.webkitTapHighlightColor = 'rgba(0,0,0,0)';
				document.documentElement.style.webkitTouchCallout = 'none';
				
				function ClickGeneralArea(){{
				//alert(event.srcElement.tagName);
				if(event.srcElement.tagName!='A')
				   window.location = '#command:ShowMenu';
				}}
				</script>";

				string html = string.Format (@"{0}{1}<body>{2}</body>", 
				StyleSheet.CSS,
				clearEffects,
				ComDomain.Instance ().GetCommentaryHTML (ZeroBasedBookID, ZeroBasedChapterID));
			
			WebView.LoadHtmlString (html, path);
			this.NavigationItem.Title = comDomain.GetTitle (Settings.ZeroBasedSelectedBookID, Settings.ZeroBasedSelectedChapterID);
	
		}
		#endregion
		
		#region Helper: MoveToNextChapter
		private void MoveToNextChapter ()
		{
			int zeroBasedSelectedBookID = Settings.ZeroBasedSelectedBookID;
			int zeroBasedChapterID = Settings.ZeroBasedSelectedChapterID;
			
			comDomain.GetNextChapterIDs (ref zeroBasedSelectedBookID,ref zeroBasedChapterID);
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion
		
		#region Helper: MoveToPreviousChapter
		private void MoveToPreviousChapter ()
		{
			int zeroBasedSelectedBookID = Settings.ZeroBasedSelectedBookID;
			int zeroBasedChapterID = Settings.ZeroBasedSelectedChapterID;
			
			comDomain.GetPreviousChapterIDs (ref zeroBasedSelectedBookID,ref zeroBasedChapterID);
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion
		
		#region Event: ChapterPicked
		public void ChapterPicked (int zeroBasedSelectedBookID, int zeroBasedChapterID)
		{
			Settings.ZeroBasedSelectedBookID = zeroBasedSelectedBookID;
			Settings.ZeroBasedSelectedChapterID = zeroBasedChapterID;
			
			ShowChapter (zeroBasedSelectedBookID, zeroBasedChapterID);
		}
		#endregion
		
	}
}
